package cn.doitedu.vueapi.controller.tg;


import cn.doitedu.vueapi.pojo.tg.*;
import cn.doitedu.vueapi.service.tg.AdGenRecServieImpl;
import cn.doitedu.vueapi.service.tg.AdResourceServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class AdResourceController {


    @Autowired
    AdResourceServiceImpl adResourceService;


    @Autowired
    AdGenRecServieImpl adGenRecServie;

    @CrossOrigin
    @RequestMapping("/ads/list")
    public AdrResult getAdList(Integer uid,String name ){

        AdResource adr = new AdResource();
        List<AdResource> lst = adResourceService.getAdList(adr);

        ArrayList<AdVo> ads = new ArrayList<>();
        for (AdResource ad : lst) {
            AdVo vo = new AdVo();
            vo.setAdid(ad.getId());
            vo.setAdimg("/tg/static/origin/" + ad.getMbImg());
            vo.setAdname(ad.getAdname());
            vo.setAdwenan(ad.getMbText());
            ads.add(vo);
        }

        AdrResult result = new AdrResult();
        result.setAds(ads);
        result.setTotal(ads.size());
        return result;
    }


    @CrossOrigin
    @RequestMapping("/ads/gen")
    public Result genMyAd(Integer uid, Integer adid){

        AdResource adr = new AdResource();
        adr.setId(adid);
        String s = adResourceService.genChannelAd(adr, uid);

        Result<String> res = new Result<>();
        res.setCode(200);
        res.setMsg("/tg/static/channels/" + s);

        return res;
    }

    @CrossOrigin
    @RequestMapping("/ads/my")
    public Result getMyAd(Integer uid,Integer adid){

        AdGenRec myAd = adGenRecServie.getMyAd(uid, adid);
        Result<String> result = new Result<>();
        result.setCode(200);
        result.setMsg(myAd == null ? "no":"/tg/static/channels/"+myAd.getPosImg());
        return result;
    }

}
